#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>

#include "patch.h"

// ExCommanButton if index >= 5 and ExCommanNo not 6 or 7 is ExCommanButton, else is mainCommanButton
// &0xFFFFFFFE    11111111 11111111 11111111 11111110
// 6                                              110
// ExCommanCount  max = 4;
// ExCommanNo     [0],   [1],   [2],   [3];
// Example        110,   111,   10111, 101000;   (二进制) ExCommanNo
//                6,     7,     23,     40;       (十进制) ExCommanNo （ExCommanNo-21）

#define N_MAIN_COMMAND_BUTTONS 7
#define N_EX_COMMAND_BUTTONS 4
#define N_OBSOLT_COMMAND_BUTTONS 2
#define IS_MAIN_COMMAND_BUTTON(addr) (addr >= MAIN_COMMAND_BUTTONS_ADDRESS && addr < MAIN_COMMAND_BUTTONS_ADDRESS + sizeof(commButtonClass) * N_MAIN_COMMAND_BUTTONS)
#define IS_EX_COMMAND_BUTTON(addr) (addr >= EX_COMMAND_BUTTONS_ADDRESS && addr < EX_COMMAND_BUTTONS_ADDRESS + sizeof(commButtonClass) * N_EX_COMMAND_BUTTONS)
#define IS_OBSOLT_COMMAND_BUTTON(addr) (addr >= OBSOLT_COMMAND_BUTTONS_ADDRESS && addr < OBSOLT_COMMAND_BUTTONS_ADDRESS + sizeof(commButtonClass) * N_OBSOLT_COMMAND_BUTTONS)
#define IS_NEWUI_BUTTON(button) (IS_MAIN_COMMAND_BUTTON((int)button) || IS_EX_COMMAND_BUTTON((int)button) || IS_OBSOLT_COMMAND_BUTTON((int)button))

SDL_Surface *BattleMenuSelect_surf = NULL;

int (*commButtonClass_draw)(commButtonClass *this, int a2, int a3);
int (*commButtonClass_drawButtonText)(commButtonClass *result, int a2, int a3, const char *a4, SDL_Color *color);

int j_commButtonClass_draw(commButtonClass *this, int dst_x, int dst_y)
{
    // printf("commButtonClass_draw: ForceCommandMenu: %d, ForceObsoltMenu: %d, ForceChAttrMenu: %d, MainMenu_selectitem: %d, ObsoltSel: %d\n",
    //              *ForceCommandMenu, *ForceObsoltMenu, *ForceChAttrMenu, *MainMenu_selectitem, *ObsoltSel);

    if (BattleMenuSelect_surf && IS_NEWUI_BUTTON(this))
    {
        int is_clicked = this->is_clicked; // 备份点击信息
        this->is_clicked = 0;              // 点击信息设为0以禁用clicked图

        // 绘制原图
        int ret = commButtonClass_draw(this, dst_x, dst_y);
        this->is_clicked = is_clicked; // 还原点击信息

        int is_sel_item = 0;

        if (*ForceObsoltMenu == 1)
            is_sel_item = ((int)this == OBSOLT_COMMAND_BUTTONS_ADDRESS + sizeof(commButtonClass) * (*ObsoltSel - 1));
        else
            is_sel_item = (this->item_id == *MainMenu_selectitem);

        if (is_sel_item)
        {
            // 绘制选择框
            SDL_Rect dst_rect;
            dst_rect.x = dst_x;
            dst_rect.y = dst_y;
            dst_rect.w = dst_x + BattleMenuSelect_surf->w;
            dst_rect.h = dst_y + BattleMenuSelect_surf->h;
            SDL_UpperBlit(BattleMenuSelect_surf, 0, *SS2D_main_surface, &dst_rect);
        }

        return ret;
    }

    return commButtonClass_draw(this, dst_x, dst_y);
}

int j_commButtonClass_drawButtonText(commButtonClass *this, int a2, int a3, const char *a4, SDL_Color *a5)
{
    if (BattleMenuSelect_surf && IS_NEWUI_BUTTON(this))
    {
        // 禁用clicked颜色
        return commButtonClass_drawButtonText(this, a2, a3, a4, NULL);
    }

    return commButtonClass_drawButtonText(this, a2, a3, a4, a5);
}

int initPatchCommButtonClass()
{
    BattleMenuSelect_surf = IMG_Load(DATA_PATH "/Resource/BattleMenuSelect.png");

    commButtonClass_draw = (void *)so_symbol(&so_mod, "_ZN15commButtonClass4drawEii");
    commButtonClass_drawButtonText = (void *)so_symbol(&so_mod, "_ZN15commButtonClass14drawButtonTextEiiPKcP9SDL_Color");

    hook_addr((uintptr_t)(LOAD_ADDRESS + 0x00108BE0), (uintptr_t)&j_commButtonClass_draw);
    hook_addr((uintptr_t)(LOAD_ADDRESS + 0x00108CD0), (uintptr_t)&j_commButtonClass_drawButtonText);

    return 0;
}